<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {margin: 0;overflow: hidden;}
    #canvas {background-color: antiquewhite;}
  </style>
</head>
<body>
  <canvas id="canvas"></canvas>
  <script>
    /** @type {HTMLCanvasElement} */
    const canvas = document.querySelector('#canvas');
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    const ctx = canvas.getContext('2d');
    ctx.lineWidth = 40;

    /*
      1.建立渐变对象，定义渐变的区域
      径向渐变: createRadialGradient(x1,y1,r1,x2,y2,r2);
    */
    const gr = ctx.createRadialGradient(300,300,50,400,400,300);

    /*
      2.为渐变添加颜色接点
      addColorStop(position, color)
    */
    gr.addColorStop(0, 'red');
    gr.addColorStop(0.5, 'yellow');
    gr.addColorStop(1, 'green');

    /*
      3.为样式赋值
    */
    ctx.fillStyle = gr;

    /*
      4.绘图
    */
    ctx.fillRect(0,0,700,700);

  </script>
</body>
</html>